package com.wifi.wifi3.utils;

import com.wifi.wifi3.AutoAllWifiDemo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author ZhangYue26
 * @date 2020-07-13 10:44
 * @Description: 密码字典生成工具
 */
public class DictionaryUtils {

    private static List<String> zeroList = new ArrayList<>();
    static {
        zeroList.add("");
        zeroList.add("0");
        zeroList.add("00");
        zeroList.add("000");
        zeroList.add("0000");
        zeroList.add("00000");
        zeroList.add("000000");
        zeroList.add("0000000");
        zeroList.add("00000000");
        zeroList.add("000000000");
        zeroList.add("0000000000");
        zeroList.add("00000000000");
        zeroList.add("000000000000");
        zeroList.add("0000000000000");
        zeroList.add("00000000000000");
        zeroList.add("000000000000000");
        zeroList.add("0000000000000000");
        zeroList.add("00000000000000000");
    }
    

    /**
     * @Author ZhangYue26
     * @Description: 生成纯数字密码字典
     * @Description //TODO
     * @Date 2020/7/13 10:57
     * @Param [minLength, maxLength],生成最小长度与最大长度纯数字
     * @return void
     **/
    public static void generateNumbersFile(int minLength,int maxLength) throws IOException {
        long useTime = 0;//生成字典时即将使用的字节数
        for (int len = minLength; len <= maxLength; len++) {
            useTime += (long) Math.pow(10, len)*100;
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("大概需要："+useTime+"毫秒--"+useTime/60+"秒--"+useTime/60/60+"小时--"+useTime/60/60/24+"天--"+useTime/60/60/24/365+"年");
        System.out.println("是否继续生成？输入“是”或“1”或“yes”或不输入表示继续破解");
        String isTrue = scanner.next();
        
        if (isTrue.equals("") || isTrue.trim().equals("是") || isTrue.trim().toLowerCase().equals("yes") || isTrue.trim().equals("1")) {
            //继续生成
            System.out.println("破解中。。。");
            for (int len = minLength; len <= maxLength; len++) {
                generateNumbersFile(len);
            }
            System.out.println("字典生成成功");
        }
        System.out.println("破解完成");
    }


    /**
     * @Author ZhangYue26
     * @Description: 生成长度为len的字典文件
     * @Description //TODO
     * @Date 2020/7/13 11:15
     * @Param [len, filePath]
     * @return void
     **/
    public static void generateNumbersFile(int len) throws IOException {
        long number = (long) Math.pow(10, len);
        for (long i = 0; i < number; i++) {
            String value = String.valueOf(i);
            if (len > value.length()) {
                value = zeroList.get(len-value.length()) + value;
            }
            AutoAllWifiDemo.crackWIFI(value);//直接进行密码爆破
        }


    }


}
